package com.hzh.cusviewdemo.Util;

import android.text.format.DateFormat;
import android.util.Log;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.hzh.cusviewdemo.Base.MyApplication;

import java.io.File;
import java.util.Date;

/**
 * 上传工具类，用于上传任意文件到阿里OSS存储
 *
 * @version 1.0.0
 */
public class UploadHelper {

    private static final String TAG = UploadHelper.class.getSimpleName();
    // http://oss-cn-hongkong.aliyuncs.com  http://oss-cn-beijing.aliyuncs.com
    private static final String ENDPOINT = "http://oss-cn-hongkong.aliyuncs.com";

    // ksdapp的用户（ksdappself@1709311817575670.onaliyun.com）包括对应的用户组->已经移除权限->AiliyunOssAccess+AdministratorAccess 下次用记得添加

    // 上传的仓库名 kauichat ksdapp
    private static final String BUCKET_NAME = "kauichat";


    private static OSS getClient() {
//        ClientConfiguration configuration = new ClientConfiguration();
//        configuration.setConnectionTimeout(5000);

        // 以下beijing - ksdapp的bucket
        // LTAI5tSCR5GLKftuaFjPFkTd
        // eAWVqJc3ceOeONLmG6IXf2J7wyYabl

        // 以下kauichat bucket
        // LTAI5t9krAJ4SU2g42pdX9QZ
        // tGSPZw2KKTWRQ3RbCFLmyqFWm4Up7n

        OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(
                "LTAI5t9krAJ4SU2g42pdX9QZ", "tGSPZw2KKTWRQ3RbCFLmyqFWm4Up7n");
        //      LTAIYQD07p05pHQW  2txxzT8JXiHKEdEjylumFy6sXcDQ0G
        return new OSSClient(MyApplication.getContext(), ENDPOINT, credentialProvider);
    }


    /**
     * 上传的最终方法，成功返回则一个路径
     *
     * @param objKey 上传上去后，在服务器上的独立的KEY
     * @param path   需要上传的文件的路径
     * @return 存储的地址
     */
    private static String upload(String objKey, String path) {
        // 构造一个上传请求
        PutObjectRequest request = new PutObjectRequest(BUCKET_NAME,
                objKey, path);
        try {
            // 初始化上传的Client
            OSS client = getClient();
            // 开始同步上传
            PutObjectResult result = client.putObject(request);
            // 得到一个外网可访问的地址
            String url = client.presignPublicObjectURL(BUCKET_NAME, objKey);
            // 格式打印输出
            Log.d(TAG, String.format("PublicObjectURL:%s", url));
            return url;
        } catch (Exception e) {
            e.printStackTrace();
            // 如果有异常则返回空
            return null;
        }
    }

    /**
     * 上传音频
     *
     * @param path 本地地址
     * @return 服务器地址
     */
    public static String uploadAudio(String path) {
        String key = getAudioObjKey(path);
        return upload(key, path);
    }

    /**
     * 分月存储，避免一个文件夹太多
     *
     * @return yyyyMM
     */
    private static String getDateString() {
        return DateFormat.format("yyyy-MM-dd", new Date()).toString();
    }

    // 获取文件名称，规划范
    private static String getAudioObjKey(String path) {
        String fileMd5 = HashUtil.getMD5String(new File(path));
        String dateString = getDateString();
        return String.format("audio/%s/%s.mp3", dateString, fileMd5);
    }
}
